Testing
Testing
- https://laravel.com/docs/testing
- We use PEST as a test framework:
- How to run tests:
- All tests:
artisan test - All tests inside a file:
artisan test --filter="AutomaticLoginForUserByIpTest" - Single test in a file:
artisan test --filter="the user can register"
- All tests:
Pest datasets (scoped per folder)
Use folder-scoped datasets to keep data close to tests and avoid manual loads in Pest.php (Pest Datasets).
- Create
tests/Unit/Datasets.php(ortests/Feature/.../Datasets.php) and define datasets withdataset('name', ...). - Pest automatically loads
Datasets.phpwithin that folder and scopes it to tests in the folder.
Minimal example:
// tests/Unit/Datasets.php
dataset('htmlSanitizerDocumentCase', fn () => [[[
'input' => '<!DOCTYPE html><html>...</html>',
'anchorCount' => 6,
]]]);
In the test:
it('sanitizes html', function (array $case) {
$clean = HtmlSanitizer::sanitize($case['input']);
expect($clean)->not()->toContain('<html>');
})->with('htmlSanitizerDocumentCase');
Notes:
- If your test signature is
function (array $case), each dataset row must be a single argument: use[[[ ... ]]]. - Avoid
glob()orrequireintests/Pest.php. PreferDatasets.phpper folder ortests/Datasets/*.phpfor truly global datasets.